home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / ici / ici.cpi / icimain.c < prev    next >
C/C++ Source or Header  |  1994-10-27  |  6KB  |  281 lines

  1. #include "ptr.h"
  2. #include "exec.h"
  3. #include "file.h"
  4. #include "str.h"
  5. #include "struct.h"
  6. #include "buf.h"
  7. #include "wrap.h"
  8. #include "func.h"
  9.  
  10. int
  11. ici_main(argc, argv)
  12. int    argc;
  13. char    *argv[];
  14. {
  15.     register int    i;
  16.     register int    j;
  17.     char        *s;
  18.     char        *fmt;
  19.     char        *arg0;
  20.     array_t        *av;
  21.     FILE        *stream;
  22.     file_t        *f;
  23.  
  24.     if
  25.     (
  26.     ici_init()
  27.     ||
  28.     mkvar(structof(v_top[-1])->s_super, "stdin",  'u', stdin)
  29.     ||
  30.         mkvar(structof(v_top[-1])->s_super, "stdout", 'u', stdout)
  31.     ||
  32.         mkvar(structof(v_top[-1])->s_super, "stderr", 'u', stderr)
  33.     )
  34.     goto fail;
  35.  
  36.     /*
  37.      * Process arguments.  Two pass, first gather "unused" arguments,
  38.      * ie, arguments which are passed into the ICI code.  Stash these in
  39.      * the array av.  NB: must be in sync with the second pass below.
  40.      */
  41.     if ((av = new_array()) == NULL)
  42.     goto fail;
  43.     if (pushcheck(av, 1))
  44.     goto fail;
  45.     *av->a_top++ = objof(&o_null); /* Leave room for argv[0]. */
  46.     arg0 = NULL;
  47.     if (argc > 1 && argv[1][0] != '-')
  48.     {
  49.     /*
  50.      * Usage1: ici file [args...]
  51.      */
  52.     arg0 = argv[1];
  53.     for (i = 2; i < argc; ++i)
  54.     {
  55.         if (pushcheck(av, 1))
  56.         goto fail;
  57.         if ((*av->a_top = objof(get_cname(argv[i]))) == NULL)
  58.         goto fail;
  59.         ++av->a_top;
  60.     }
  61.     }
  62.     else
  63.     {
  64.     /*
  65.      * Usage2: ici [-f file] [-p prog] etc... [--] [args...]
  66.      */
  67.     for (i = 1; i < argc; ++i)
  68.     {
  69.         if (argv[i][0] == '-')
  70.         {
  71.         for (j = 1; argv[i][j] != '\0'; ++j)
  72.         {
  73.             switch (argv[i][j])
  74.             {
  75.             case 'm':
  76.             if (argv[i][++j] != '\0')
  77.                 s = &argv[i][j];
  78.             else if (++i >= argc)
  79.                 goto usage;
  80.             else
  81.                 s = argv[i];
  82.             if ((av->a_base[0] = objof(get_cname(s))) == NULL)
  83.                 goto fail;
  84.             break;
  85.  
  86.             case '-':
  87.             while (++i < argc)
  88.             {
  89.                 if (pushcheck(av, 1))
  90.                 goto fail;
  91.                 if ((*av->a_top = objof(get_cname(argv[i])))==NULL)
  92.                 goto fail;
  93.                 ++av->a_top;
  94.             }
  95.             break;
  96.  
  97.             case 'f':
  98.             case 'l':
  99.             case 'e':
  100.             if (argv[i][++j] != '\0')
  101.                 arg0 = &argv[i][j];
  102.             else if (++i >= argc)
  103.                 goto usage;
  104.             else
  105.                 arg0 = argv[i];
  106.             break;
  107.  
  108.             case 'w':
  109.             if (argv[i][++j] != '\0')
  110.                 s = &argv[i][j];
  111.             else if (++i >= argc)
  112.                 goto usage;
  113.             else
  114.                 s = argv[i];
  115.             break;
  116.  
  117.             case '0': case '1': case '2': case '3': case '4':
  118.             case '5': case '6': case '7': case '8': case '9':
  119.             continue;
  120.  
  121.             default:
  122.             goto usage;
  123.             }
  124.             break;
  125.         }
  126.         }
  127.         else
  128.         {
  129.         if (pushcheck(av, 1))
  130.             goto fail;
  131.         if ((*av->a_top = objof(get_cname(argv[i]))) == NULL)
  132.             goto fail;
  133.         ++av->a_top;
  134.         }
  135.     }
  136.     }
  137.     if (av->a_base[0] == objof(&o_null))
  138.     {
  139.     if (arg0 == NULL)
  140.         arg0 = argv[0];
  141.     if ((av->a_base[0] = objof(get_cname(arg0))) == NULL)
  142.         goto fail;
  143.     }
  144.     else
  145.     arg0 = stringof(av->a_base[0])->s_chars;
  146.     if (av->a_top - av->a_base == 2 && stringof(av->a_base[1])->s_nchars == 0)
  147.     --av->a_top; /* Patch around Bourne shell "$@" with no args bug. */
  148.  
  149.     if
  150.     (
  151.     mkvar(structof(v_top[-1])->s_super, "argv", 'o', objof(av))
  152.     ||
  153.     mkvar(structof(v_top[-1])->s_super, "argc", 'i', (long)(av->a_top - av->a_base))
  154.     )
  155.     goto fail;
  156.     
  157.     /*
  158.      * Pass two over the arguments; actually parse the modules.
  159.      */
  160.     if (argc > 1 && argv[1][0] != '-')
  161.     {
  162.     if ((stream = fopen(argv[1], "r")) == NULL)
  163.     {
  164.         sprintf(buf, "%s: Could not open %s.", argv[0], argv[1]); 
  165.         error = buf;
  166.         goto fail;
  167.     }
  168.     if (parse_file(argv[1], (char *)stream, &stdio_ftype))
  169.         goto fail;
  170.     }
  171.     else
  172.     {
  173.     for (i = 1; i < argc; ++i)
  174.     {
  175.         if (argv[i][0] != '-')
  176.         continue;
  177.         if (argv[i][1] == '\0')
  178.         {
  179.         if (parse_file("stdin", (char *)stdin, &stdio_ftype))
  180.             goto fail;
  181.         continue;
  182.         }
  183.         for (j = 1; argv[i][j] != '\0'; ++j)
  184.         {
  185.         switch (argv[i][j])
  186.         {
  187.         case '-':
  188.             i = argc;
  189.             break;
  190.  
  191.         case 'e':
  192.             if (argv[i][++j] != '\0')
  193.             s = &argv[i][j];
  194.             else if (++i >= argc)
  195.             goto usage;
  196.             else
  197.             s = argv[i];
  198.             if ((f = sopen(s, strlen(s))) == NULL)
  199.             goto fail;
  200.             if ((f->f_name = get_cname("")) == NULL)
  201.             goto fail;
  202.             if (parse_module(f, structof(v_top[-1])) < 0)
  203.             goto fail;
  204.             loose(f);
  205.             break;
  206.  
  207.         case 'l':
  208. #ifdef    MSDOS
  209.             fmt = "C:\\ICI\\LIB%s.ICI";
  210. #else
  211.             fmt = "/usr/local/include/ici/%s.ici";
  212. #endif
  213.             goto dofile;
  214.  
  215.         case 'f':
  216.             fmt = "%s";
  217.         dofile:
  218.             if (argv[i][++j] != '\0')
  219.             s = &argv[i][j];
  220.             else if (++i >= argc)
  221.             goto usage;
  222.             else
  223.             s = argv[i];
  224.             if (chkbuf(strlen(s) + strlen(fmt)))
  225.             goto fail;
  226.             sprintf(buf, fmt, s);
  227.             if ((stream = fopen(buf, "r")) == NULL)
  228.             {
  229.             sprintf(buf, "%s: Could not open %s.", argv[0], s); 
  230.             error = buf;
  231.             goto fail;
  232.             }
  233.             if (parse_file(buf, (char *)stream, &stdio_ftype))
  234.             goto fail;
  235.             break;
  236.  
  237.         case '0': case '1': case '2': case '3': case '4':
  238.         case '5': case '6': case '7': case '8': case '9':
  239.             if ((stream = fdopen(argv[i][j] - '0', "r")) == NULL)
  240.             {
  241.             sprintf(buf, "%s: Could not access file descriptor %d.",
  242.                 argv[0], argv[i][j] - '0'); 
  243.             error = buf;
  244.             goto fail;
  245.             }
  246.             if (parse_file(arg0, (char *)stream, &stdio_ftype))
  247.             goto fail;
  248.             continue;
  249.         }
  250.         break;
  251.         }
  252.     }
  253.     }
  254. /*
  255.  * For tracking down object leaks...
  256. got(fetch(v_top[-1], get_cname("stdout")));
  257. v_top = vs->a_base;
  258. o_top = os->a_base;
  259. x_top = xs->a_base;
  260. ICI_reclaim();
  261. ICI_reclaim();
  262. pause();
  263. */
  264.     wrapup();
  265.     return 0;
  266.  
  267. usage:
  268.     fprintf(stderr, "usage:\t%s [-f file] [-e prog] [-digit] [-m name] [--] args...\n", argv[0]);
  269.     fprintf(stderr, "\t%s file args...\n", argv[0]);
  270.     wrapup();
  271.     error = "invalid command line arguments";
  272.     return 1;
  273.  
  274. fail:
  275.     s = error;
  276.     wrapup();
  277.     fflush(stdout);
  278.     fprintf(stderr, "%s\n", s);
  279.     return 1;
  280. }
  281.